package com.stock.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stock.annotation.RoleEnum;
import com.stock.annotation.Roles;
import com.stock.config.WebConfig;
import com.stock.entity.Record;
import com.stock.entity.Result;
import com.stock.service.IRecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    IRecordService recordService;

    @ApiOperation("获取股票列表")
    @PostMapping("/list")
    public Result list(@RequestBody Map<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        String searchStockName = map.get("searchStockName");
        String searchTime = map.get("searchTime");
        String username = map.get("username");

        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username).like("stock_name", searchStockName).like("create_time", searchTime);

        Page<Record> page = new Page<>(pageNum, WebConfig.PAGE_SIZE);
        Page<Record> recordPage = recordService.page(page, wrapper);
        List<Record> recordList = recordPage.getRecords();
        long total = recordPage.getTotal();
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("recordList", recordList);
        jsonObject.putOpt("total", total);

        return Result.success(jsonObject, "股票列表获取成功");
    }

    @Roles(RoleEnum.Admin)
    @ApiOperation("获取股票列表")
    @PostMapping("/listAll")
    public Result listAll(@RequestBody Map<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        String searchStockName = map.get("searchStockName");
        String searchTime = map.get("searchTime");

        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.like("stock_name", searchStockName).like("create_time", searchTime);

        Page<Record> page = new Page<>(pageNum, WebConfig.PAGE_SIZE);
        Page<Record> recordPage = recordService.page(page, wrapper);
        List<Record> recordList = recordPage.getRecords();
        long total = recordPage.getTotal();
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("recordList", recordList);
        jsonObject.putOpt("total", total);

        return Result.success(jsonObject, "股票列表获取成功");
    }
}
